<?php
/**
 * @version 1.0
 * @created 08-Jun-2009 3:16:47 PM
 */
class UpdateUser
{

	static $dataValidator;

	function __construct()
	{
	}



	/**
	 * 
	 * @param user
	 */
	static function updateUser( User $user )
	{
		// Instantiate the AmazonSDB class
		$sdb = new AmazonSDB();
		// Store the name of the domain
		$domain = USERS_DOMAIN;
		// Create the domain
		$user_domain = $sdb->domain_metadata( $domain );
		if ($user_domain->isOK())
		{
			//encode the user and put
			$userObj = json_decode( json_encode( $user ), true );
			//$userObj["keys"] = implode(",", $roleObj["keys"] );
			//stringify the roles as amazon DB does not support super columns
			$rolesArray = array();
			foreach( $user->roles as $role ){
				array_push( $rolesArray, $role );
			}
			$userObj["roles"] = json_encode( $rolesArray );
			$response = $sdb->put_attributes( $domain, $user->fbID, $userObj, true );
			if ($response->isOK())
			{
				if( Cache::getInstance()->get($user->fbID) != false ){
					//error_log("updating user in cache", 0);
					Cache::getInstance()->replace($user->fbID, $user, false, 86400);
				}else{
					//error_log("adding user in cache", 0);
					Cache::getInstance()->add($user->fbID, $user, false, 86400);
				}
				return AwsUtils::parseResponse( $response );
			}else{
				return AwsUtils::parseError( $response );
			}
		}else{
			return AwsUtils::parseError( $user_domain );
		}
	}

}
?>